package com.xuemeng.mail;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.List;
import java.util.Properties;


/**
 * @author xuemeng
 * @date 2018/12/13 11:14
 * pwd:qgyantrvozembeji
 * //第三方登陆qq密码：qgyantrvozembeji
 */
public class MailSender {
        //实例化一个mail对象
        private static MailEntity mail = new MailEntity();

        /**
         * 设置标题
         *
         * @param title
         * @return
         */
        public MailSender title(String title) {
                mail.setTitle(title);
                return this;
        }

        /**
         * 设置邮件内容
         *
         * @param content
         * @return
         */
        public MailSender content(String content) {
                mail.setContent(content);
                return this;
        }

        /**
         * 邮件内容格式
         *
         * @param typeEnum
         * @return
         */
        public MailSender contentType(MailContentTypeEnum typeEnum) {
                mail.setContentType(typeEnum.getValue());
                return this;
        }

        /**
         * 目标邮箱
         *
         * @param targets
         * @return
         */
        public MailSender target(List<String> targets) {
                mail.setList(targets);
                return this;
        }

        public void send() throws Exception {

                if (mail.getContentType() == null) {
                        mail.setContentType(MailContentTypeEnum.HTML.getValue());
                }
                if (mail.getContent() == null || mail.getContent().trim().length() == 0) {
                        throw new Exception("请填写邮件内容");
                }
                if (mail.getTitle() == null || mail.getTitle().trim().length() == 0) {
                        throw new Exception("请填写邮件标题");
                }
                if (mail.getList().size() == 0) {
                        throw new Exception("请填写要发送的人员");
                }

                final PropertiesUtil putil = new PropertiesUtil("mail");

                final Properties prop = new Properties();
                prop.put("mail.smtp.auth", true);
              /*  prop.put("mail.smtp.starttls.enable", "true");

                //不做服务器证书校验
                prop.put("mail.smtp.ssl.checkserveridentity", "false");

                //添加信任的服务器地址，多个地址之间用空格分开
                prop.put("mail.smtp.ssl.trust", putil.getValue("mail.smtp.service"));*/

                prop.put("mail.smtp.host", putil.getValue("mail.smtp.service"));
                prop.put("mail.smtp.port", putil.getValue("mail.smtp.port"));
                prop.put("mail.user", putil.getValue("mail.from.address"));
                prop.put("mail.password", putil.getValue("mail.from.smtp.pwd"));
                String username = prop.getProperty("mail.user");
                String password = prop.getProperty("mail.password");

           /*     Authenticator acthenticator=new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                                String username=prop.getProperty("mail.user");
                                String pawwword=prop.getProperty("mail.password");
                                return new PasswordAuthentication(username,pawwword);
                        }
                };
*/
                Session mailsession = Session.getInstance(prop, new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                                return new PasswordAuthentication(username, password);
                        }
                });

                MimeMessage message = new MimeMessage(mailsession);

                String nickname = MimeUtility.encodeText(putil.getValue("mail.from.nickname"));

                InternetAddress address = new InternetAddress(nickname + "<" + prop.getProperty("mail.user") + ">");
                message.setFrom(address);
                message.setSubject(mail.getTitle());
                if (mail.getContentType().equals(MailContentTypeEnum.HTML.getValue())) {
                        message.setContent(mail.getContent(), mail.getContentType());
                } else if (mail.getContentType().equals(MailContentTypeEnum.TEXT.getValue())) {
                        message.setText(mail.getContent());
                }
                List<String> targets = mail.getList();
                for (int i = 0; i < targets.size(); i++) {
                        try {
                                InternetAddress to = new InternetAddress(targets.get(i));
                                message.setRecipient(Message.RecipientType.TO, to);


                                Transport.send(message);
                        } catch (Exception e) {
                                new Exception("发送邮件失败");
                                e.printStackTrace();
                        }
                }




        }




}
